﻿<?php
/***********************************************************
    [EasyTalk] (C)2009 - 2011 nextsns.com
    This is NOT a freeware, use is subject to license terms

    @Filename SettingAction.class.php $

    @Author hjoeson $

    @Date 2011-01-09 08:45:20 $
*************************************************************/

class FuwudanAction extends Action {

    public function _initialize() {
        $this->tologin();
		parent::init();
        $this->assign('usertemp',usertemp($this->my));
        $this->assign('menu','fuwudan');
        $this->assign('subname','服务单集');
		$this->ctent=D('Content');
        //$this->ctent->setmy($this->my);
        $this->assign('ctent',$this->ctent);
    }

	// 6/23/2011 create fuwudan sub-folder.
	public function index(){	
		$fuwudanid=$_GET['id'];
		
		 if(!empty($fuwudanid)) {	
				$fwview=D('FuwudanView');
				$map['fuwudanid']=$fuwudanid;
				$result_fuwudan=$fwview->where($map)->find();
				$this->assign('vo_fuwudan',$result_fuwudan);
				//echo $vo_fuwudan['fuwujiage'];
				$this->display();
				header('location:'.SITE_URL.'/fuwudan/list');
			}else{
				//Cookie::set('setok','fuwudan_none');
				//exit;
				$this->error('服务单不存在！');
			}
		
	}

 	//定义服务单的集合。查询处理等。 6/19/2011 Luozhengfeng  6/22/2011 移到新加的fuwu功能中。
	 public function dlist(){
		$fuwudanlist =D('FuwudanView');
		$condition_fuwudanlist=" and fuwudan_status!='已完成' and fuwudan_status!='已关闭'  ";
		$order_fuwudanlist="fuwudanid desc";
		
		//接收并定义我的单据 Luozhengfeng 6/26/2011	
		$mines=$_GET['mines'];
		if($mines=="on"){
			$condition_fuwudanlist=$condition_fuwudanlist.' and Users.user_id='.$this->my['user_id'];
		}else{
			$condition_fuwudanlist=$condition_fuwudanlist.''; 	
		}		
		

		//接收并定义我所买的单据 Luozhengfeng 6/26/2011
		$minebuy=$_GET['minebuy'];
		if($minebuy=="on"){
			$condition_fuwudanlist=$condition_fuwudanlist.' and Fuwudan.purchase_userid='.$this->my['user_id'];
		}else{
			$condition_fuwudanlist=$condition_fuwudanlist.''; 	
		}

		
		//接收并定义所有的单据，包含已关闭或是已完成的单据! Luozhengfeng 6/26/2011
		$afwd=$_GET['afwd'];
		if($afwd=="on"){
			$condition_fuwudanlist='';
		}else{
			$condition_fuwudanlist=$condition_fuwudanlist.''; 	
		}

		//接收并定义评价为好的单据! Luozhengfeng 6/26/2011
		$good=$_GET['good'];
		if($good=="on"){
			$condition_fuwudanlist=' and fuwudan_pingfen>7 ';
		}else{
			$condition_fuwudanlist=$condition_fuwudanlist.''; 	
		}

		//接收并定义评价为差的单据! Luozhengfeng 6/26/2011
		$bad=$_GET['bad'];
		if($bad=="on"){
			$condition_fuwudanlist=' and fuwudan_pingfen=2 ';
		}else{
			$condition_fuwudanlist=$condition_fuwudanlist.''; 	
		}

		//echo $condition_fuwudanlist;


		import("@.ORG.Page"); //导入分页类
		$count = $fuwudanlist->where('Fuwudan.fuwudanid>0 '.$condition_fuwudanlist)->count();    //计算总数		
		$p = new Page( $count,10 );		
		$result_fuwudanlist=$fuwudanlist->where("Fuwu.fuwuid>0".$condition_fuwudanlist)->limit($p->firstRow.','.$p->listRows)->order($order_fuwudanlist)->findall();	
		$p->setConfig('header','个服务单');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');

		
		if($result_fuwudanlist){			
				$this->assign('volfuwudanlist',$result_fuwudanlist);
		}
		//dump($result_fuwudanlist);
	   $page = $p->show ();
	   $this->assign( "page_fuwudan", $page );
	   $this->assign('menu','fuwudan');
	   $this->assign('count',$count);
       $this->display('dlist');
	
	}
	public function fukuan($fwdid){
		if($fwdid){
			$fuwudanid=$fwdid;
		}
		if($_GET['id']){
			$fuwudanid=$_GET['id'];
		}
		 if(!empty($fuwudanid)) {	
				$fwview=D('FuwudanView');
				$map['fuwudanid']=$fuwudanid;
				$result_fuwudan=$fwview->where($map)->find();
				$this->assign('fuwudanview',$result_fuwudan);
				//dump($result_fuwudan);
				if($result_fuwudan['shifoufukuan']){
					
					//Cookie::set('setok','fuwudan_alreadyfukuan');
					//header('location:'.SITE_URL.'/Fuwudan?id='.$fuwudanid);
					//exit;
					$this->assign("jumpUrl",SITE_URL."/Fuwudan?id=".$fuwudanid);
					$this->error('服务单已付款！不需要再次付款了!');
				}else{
					//echo "购买人的id: ".$result_fuwudan['purchase_userid']."<br>";
					//echo "当前登录用户的id: ".$this->my['user_id']."<br>";
					if($result_fuwudan['purchase_userid']==$this->my['user_id']){
						$yingfujinger_fuwudan = $result_fuwudan['yingfujinger'];
						$yongfuyuer_buy = M('Users')->where("user_id='".$result_fuwudan['purchase_userid']."'")->getField('yongfuyuer'); 				
						$yongfuyuer_sell = M('Users')->where("user_id='".$result_fuwudan['user_id']."'")->getField('yongfuyuer'); 
						$yonghufukuanshu = M('Fuwu')->where("fuwuid='".$result_fuwudan['fuwuid']."'")->getField('yonghufukuanshu');
						$suodebaochou = M('Fuwu')->where("fuwuid='".$result_fuwudan['fuwuid']."'")->getField('suodebaochou'); 
						if($yongfuyuer_buy>=$yingfujinger_fuwudan){

							//echo "<script> alert('你确定要付款吗?您的余额将会被扣除!') </script>";	
							
								$data['yongfuyuer']=$yongfuyuer_buy-$yingfujinger_fuwudan;
								M('Users')->where("user_id='".$result_fuwudan['purchase_userid']."'")->data($data)->save();
								unset($data);
								$data['yongfuyuer']=$yongfuyuer_sell+$yingfujinger_fuwudan;							
								M('Users')->where("user_id='".$result_fuwudan['user_id']."'")->data($data)->save();
								unset($data);
								$data['yonghufukuanshu']=$yonghufukuanshu+1; //用户付款数+1 6/25/2011 yonghufukuanshu
								//echo $data['yonghufukuanshu'];
								$data['suodebaochou']=$suodebaochou+$yingfujinger_fuwudan;
								M('Fuwu')->where("fuwuid='".$result_fuwudan['fuwuid']."'")->data($data)->save();
								unset($data);
								$data['shifoufukuan']=1;
								$data['fukuanshijian']=time();
								$data['fuwudan_updatetime']=time();
								M('Fuwudan')->where("fuwudanid='".$result_fuwudan['fuwudanid']."'")->data($data)->save();
								unset($data);
							
							
							
						}else{
							
							//Cookie::set('setok','fuwudan_noyuer');
							//header('location:'.SITE_URL.'/Setting/chongzhi');
							//exit;
							$this->assign("jumpUrl",SITE_URL."/Setting/chongzhi");
							$this->error('您的余额不够，请充值, '.SITE_URL.'/Setting/chongzhi');
							header('location:'.SITE_URL.'/Setting/chongzhi');
						}
					}else{
						
						//Cookie::set('setok','fuwudan_onlyyoupayforyou');
						//header('location:'.SITE_URL."/Fuwudan?id=".$fuwudanid);
						//exit;
						$this->assign("jumpUrl",SITE_URL."/Fuwudan?id=".$fuwudanid);
						$this->error('您没有开通为他人付款的功能!');
					}					
				}
				$this->assign("jumpUrl",SITE_URL."/Fuwudan?id=".$fuwudanid);
				$this->success(" 付款成功！ " ); 
				//Cookie::set('setok','fuwudan_fukuanok');
				//header('location:'.SITE_URL."/Fuwudan?id=".$fuwudanid);
			}else{				
				//Cookie::set('setok','fuwudan_none');
				//header('location:'.SITE_URL.'/Fuwudan/dlist');
				//exit;
				$this->assign("jumpUrl",SITE_URL."/Fuwudan/dlist");
				$this->error('服务单不存在！');
			}

	}

	 public function test() {
        $this->tologin();
        echo $_GET['cid'];
    }


	public function updatestatus(){
		$fuwudanid = $_POST["fuwudanid"];		
		$userid=M('Fuwudan')->where("fuwudanid='".$fuwudanid."'")->getField('user_id');	
		$fuwuid=M('Fuwudan')->where("fuwudanid='".$fuwudanid."'")->getField('fuwuid');
		if($userid==$this->my['user_id']){
			$fuwudanstatus = $_POST['fuwudanstatus'];
			$data['fuwudan_status']=$fuwudanstatus;
			$data['fuwudan_updatetime']=time();
			M('Fuwudan')->where("fuwudanid='".$fuwudanid."'")->data($data)->save();
			unset($data);
			if(fuwudanstatus=="已完成"||fuwudanstatus=="已结束"){
				 M('Fuwu')->setInc('wanchengzongshu','fuwuid='.$fuwuid,1); 
			}
			$this->assign("jumpUrl",SITE_URL."/Fuwudan?id=".$fuwudanid);
			$this->success(" 更新成功！ " ); 
			//Cookie::set('setok','fuwudan_updateok');
			//header('location:'.SITE_URL."/Fuwudan?id=".$fuwudanid);

			//header("location:".SITE_URL."/Fuwudan/dlist");
		}else{
			$this->assign("jumpUrl",SITE_URL."/Fuwudan?id=".$fuwudanid);
			$this->assign("waitSecond",1);
			$this->error('您无法改变此服务单的状态!');
			//header("location:".SITE_URL."/Fuwudan/dlist");
			//Cookie::set('setok','fuwudan_cannotchange');
			//header('location:'.SITE_URL.'/Fuwudan?id='.$fuwudanid);			
			//exit;
		}
		//$this->display();	
	}

		public function pingfen(){
		$fuwudanid = $_POST["fuwudanid"];		
		$userid=M('Fuwudan')->where("fuwudanid='".$fuwudanid."'")->getField('purchase_userid');
		$fuwuid=M('Fuwudan')->where("fuwudanid='".$fuwudanid."'")->getField('fuwuid');
		if($userid==$this->my['user_id']){
			$pingfen = $_POST['pingfen'];
			$remark = $_POST['remark'];
			$data['fuwudan_pingfen']=$pingfen;
			$data['remark']=$remark;
			$data['pingfen_time']=time();
			M('Fuwudan')->where("fuwudanid='".$fuwudanid."'")->data($data)->save();
			unset($data);
			if($pingfen>7){
				 M('Fuwu')->setInc('haopinggeshu','fuwuid='.$fuwuid,1); 
			}
			header('location:'.SITE_URL.'/Fuwudan/dlist');
		}else{
			header('location:'.SITE_URL.'/Fuwudan/dlist');
			exit;
		}
		
	}




    private function tologin() {
        if (!$this->my) {
            echo '<script type="text/javascript">window.location.href="'.SITE_URL.'/login"</script>';
            exit;
        }
    }
}
?>